package com.applet.controller.admin;

import com.applet.common.vo.Result;
import com.applet.pojo.dto.PublishActivityDTO;
import com.applet.pojo.vo.ActivityDetailsVO;
import com.applet.pojo.vo.ActivityItemVO;
import com.applet.pojo.dto.UpdateActivityDTO;
import com.applet.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author “XuShao”
 * @date 2024/12/15 17:45
 * @description
 */
@RestController
@RequestMapping("/admin/activity")
@Slf4j
@Tag(name = "活动相关接口")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Operation(summary = "查询所有活动")
    @GetMapping
    public Result<List<ActivityItemVO>> list() {
        log.info("查询所有活动");

        List<ActivityItemVO> activityItemVOS = activityService.getAllActivity();

        return Result.success(activityItemVOS);
    }

    @Operation(summary = "根据id查询活动")
    @GetMapping("/{id}")
    public Result<ActivityDetailsVO> getActivityDetails(@PathVariable String id) {
        log.info("查找id为：{} 用户的详情", id);

        ActivityDetailsVO detailsVO = activityService.selectActivityDetails(id);

        return Result.success(detailsVO);
    }

    @Operation(summary = "查询当前用户发布的活动")
    @GetMapping("history")
    public Result<List<ActivityItemVO>> getUserPublishActivity() {
        log.info("=== 查询当前用户发布的活动 ===");

        List<ActivityItemVO> activityItemVOS = activityService.getUserPublishActivity();

        return Result.success(activityItemVOS);
    }

    @Operation(summary = "新增活动")
    @PostMapping
    public Result<?> publishActivity(@RequestBody @Validated PublishActivityDTO publishActivityDTO) {
        log.info("新增活动 => {}", publishActivityDTO);

        activityService.addActivity(publishActivityDTO);

        return Result.success();
    }

    @Operation(summary = "修改活动")
    @PutMapping
    public Result<?> update(@RequestBody @Validated UpdateActivityDTO updateActivityDTO) {
        log.info("修改活动 => {}", updateActivityDTO);

        activityService.updateActivity(updateActivityDTO);

        return Result.success();
    }

    @Operation(summary = "根据id删除活动")
    @DeleteMapping("{id}")
    public Result<?> removeActivity(@PathVariable String id) {
        log.info("根据id删除活动 => {}", id);

        activityService.removeById(id);

        return Result.success();
    }
}
